﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Led.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Represents the collection of Notification Leds on the device.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using Diagnostics.Annotations;

namespace MobilePal.WindowsNative.Notification
{
  #region Imported Namespaces


  using System.ComponentModel;
  using System.Runtime.InteropServices;


  #endregion


  /// <summary>
  /// Represents the collection of Notification Leds on the device.
  /// </summary>
  /// <remarks>
  /// Support varies depending on the device but all devices should include at least 1 notification LED.
  /// </remarks>
  [UsedImplicitly(ImplicitUseFlags.IncludeMembers)]
  public class Led
  {
    #region Constants and Fields


    private const int NLED_COUNT_INFO_ID = 0;
    private const int NLED_SETTINGS_INFO_ID = 2;
    private const int NLED_SUPPORTS_INFO_ID = 1;
    private int m_count;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="Led"/> class.
    /// </summary>
    /// <exception cref="Win32Exception">
    /// Error initializing LED's.
    /// </exception>
    public Led()
    {
      var pOutput = new NLED_COUNT_INFO();
      if (!NativeMethods.NLedGetDeviceCount(0, ref pOutput))
      {
        throw new Win32Exception(Marshal.GetLastWin32Error(), "Error initializing LED's");
      }

      m_count = (int)pOutput.cLeds;
    }


    #endregion


    #region Enums


    /// <summary>
    /// Defines the possible states for an LED.
    /// </summary>
    public enum LedState
    {
      Off, 
      On, 
      Blink
    }


    #endregion


    #region Properties


    /// <summary>
    /// Returns the number of notification Leds in the system.
    /// </summary>
    public int Count
    {
      get
      {
        return m_count;
      }
    }


    #endregion


    #region Public Methods


    /// <summary>
    /// Set the state of the specified LED.
    /// </summary>
    /// <param name="led">
    /// 0 based index of the LED.
    /// </param>
    /// <param name="newState">
    /// New state of the LED - see <see cref="LedState"/> enumeration.
    /// </param>
    public void SetLedStatus(int led, LedState newState)
    {
      var pOutput = new NLED_SETTINGS_INFO();
      pOutput.LedNum = led;
      pOutput.OffOnBlink = (int)newState;
      NativeMethods.NLedSetDevice(2, ref pOutput);
    }


    #endregion


    [StructLayout(LayoutKind.Sequential)]
    internal struct NLED_COUNT_INFO
    {
      public uint cLeds;
    }


    [StructLayout(LayoutKind.Sequential)]
    internal struct NLED_SETTINGS_INFO
    {
      public int LedNum;
      public int OffOnBlink;
      public int TotalCycleTime;
      public int OnTime;
      public int OffTime;
      public int MetaCycleOn;
      public int MetaCycleOff;
    }


    [StructLayout(LayoutKind.Sequential)]
    internal struct NLED_SUPPORTS_INFO
    {
      public uint LedNum;
      public int lCycleAdjust;
      public bool fAdjustTotalCycleTime;
      public bool fAdjustOnTime;
      public bool fAdjustOffTime;
      public bool fMetaCycleOn;
      public bool fMetaCycleOff;
    }
  }
}